package com.dxq.oa.ontroller;

import com.dxq.oa.entity.Menu;
import com.dxq.oa.entity.Role;
import com.dxq.oa.entity.User;
import com.dxq.oa.pub.OaResult;
import com.dxq.oa.pub.ResponseData;
import com.dxq.oa.pub.VueData;
import com.dxq.oa.service.UserService;
import com.dxq.oa.utils.JWTUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Member;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@CrossOrigin
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public ResponseData login(@RequestBody User user, HttpServletRequest request){

        log.info("验证码信息为-------------------"+user.getCode());
        log.info("用户"+user.getUseraccount()+"请求登录.............");

        String verifyCode = (String)request.getSession().getAttribute("VerifyCode");


        try {
            if(!verifyCode.equals(user.getCode())){
                return new ResponseData("ERROR","验证码不正确");
            }
            User user1 = this.userService.login(user);
            Map<String,String> payLoad = new HashMap<>();
            payLoad.put("username",user1.getUsername());
            payLoad.put("role",user1.getRole());
            payLoad.put("pwd",user1.getPassword());
            payLoad.put("account",user1.getUseraccount());
            payLoad.put("id",String.valueOf(user1.getId()));
            List<String> list = new ArrayList<>();
            //创建token
            String token = JWTUtils.getToken(payLoad);
            list.add(token);
            ResponseData responseData = new ResponseData("SUCCESS",list,"登录成功",user1);
            return responseData;

        }catch (RuntimeException e){
            return new ResponseData("ERROR",e.getMessage());
        }
    }

    @RequestMapping("/test")
    public String test(){
        System.out.println("412234342342");
        return "ok";
    }

    //用户查询
    @GetMapping("/findall")
    public ResponseData getAll(){
        log.info("查询用户信息请求----------------------");
       List<User> list = this.userService.findAll();

       return new ResponseData("SUCCESS",list);

    }


    //分页查询
    @GetMapping("/findByPage")
    public ResponseData findByPage(Integer pagenum,Integer pageSize){

        pagenum = pagenum == null ? 1:pagenum;
        pageSize = pageSize ==null?10:pageSize;
        List<User> byPage = this.userService.findByPage(pagenum.toString(), pageSize.toString());

        long totol = this.userService.findTotol();
        log.info("总共"+totol+"条数据");

        return new ResponseData("SUCCESS",byPage,String.valueOf(totol));

    }

    @GetMapping("/menu")
    public VueData getMenu(){
        return new VueData("SUCCESS",this.userService.findMenu());
    }


    //添加用户
    @PostMapping("/addUser")
    public ResponseData addUser(@RequestBody User user){
        log.info("添加新用户---"+user.toString());

        boolean isUser = this.userService.addUser(user);

        if(isUser){
            return new ResponseData("SUCCESS","添加成功");
        }
        return new ResponseData("ERROR","添加失败");
    }

    @GetMapping("/getMenuList")
    public ResponseData getMenuList(){
        List<Menu> list = this.userService.findMenuList();
        return new ResponseData("SUCCESS",list);
    }


    //修改密码
    @PostMapping("/alterPwd")
    public OaResult alterPwd(@RequestBody String newpwd,HttpServletRequest request){

        log.info("修改密码请求--------------");
        log.info("旧密码："+newpwd.split(",")[1] + ";   新密码"+newpwd.split(",")[0]);
        try {
            this.userService.alterPwd(newpwd,request.getHeader("token"));
            return OaResult.getSuccess("修改成功，请重新登陆");
        }catch (Exception e){
            return OaResult.error(e.getMessage());
//            e.getMessage();
        }
    }

    @GetMapping("/getrole")
    public OaResult getrole(){
        List<Role> role = this.userService.getRole();

        return OaResult.getSuccessData("查询成功",role);
    }
}
